﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Core;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.Resumes
{
    public class CertificationService : ICertificationService
    {
        #region Constants
        private const string Certification_BY_ID_KEY = "certification.id-{0}";
        private const string Certification_PATTERN_KEY = "certification.";
        #endregion

        #region Fields
        private readonly IRepository<Certification> _certificationRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenerateNumberService _generateNumberService;

        #endregion

        #region Ctor
        public CertificationService(IRepository<Certification> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher, IGenerateNumberService generateNumberService)
        {
            _certificationRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
            _generateNumberService = generateNumberService;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteCertification(Certification certification)
        {
            if (certification == null)
                throw new ArgumentNullException("Certification");

            _certificationRepository.Delete(certification);

            _cacheManager.RemoveByPattern(Certification_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(certification);

        }

        public Certification GetCertificationById(Guid certificationId)
        {
            if (certificationId == Guid.Empty)
                return null;

            string key = string.Format(Certification_BY_ID_KEY, certificationId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _certificationRepository.GetById(certificationId);
                return cl;
            });
        }

        public virtual IPagedList<Certification> GetAllCertifications(int pageIndex, int pageSize)
        {
            var query = _certificationRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Certification>(query, pageIndex, pageSize);
            return items;
        }

        public virtual IList<Certification> GetCertifications(Guid resumeId)
        {
            var query = _certificationRepository.Table;
            query = query.Where(c => c.ResumeId == resumeId).OrderByDescending(c => c.LastUpdated);

            return query.ToList();
        }

        public virtual void InsertCertification(Certification certification)
        {
            if (certification == null)
                throw new ArgumentNullException("Certification");

            _certificationRepository.Insert(certification);

            _cacheManager.RemoveByPattern(Certification_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(certification);
        }

        public virtual void UpdateCertification(Certification certification)
        {
            if (certification == null)
                throw new ArgumentNullException("Certification");
            _certificationRepository.Update(certification);

            _cacheManager.RemoveByPattern(Certification_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(certification);
        }
        #endregion

        public Guid CreateId
        {
            get
            {
                return _generateNumberService.GenerateId<Certification>(_certificationRepository, GenerateType.Certification);
            }
        }

        public bool HasCertifications(Guid resumeId)
        {
            return _certificationRepository.Table.Any(s => s.ResumeId == resumeId);
        }
    }
}
